package com.dferreira.repository.data_delegators;

import android.content.Context;
import android.util.Log;

import com.dferreira.languagesteach.utils.DataPaths;
import com.dferreira.repository.RepositoryDatabaseHelper;
import com.dferreira.repository.data_models.Dependency;
import com.dferreira.repository.data_models.PackageMetadata;
import com.j256.ormlite.dao.RuntimeExceptionDao;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dferreira on 7/19/13.
 * for LanguagesTeach
 */
public class ExternalPackageQueries {

    private final String TAG = "ExternalPackageQueries";

    private final Context context;
    private final DataPaths dataPaths;
    private RepositoryDatabaseHelper repositoryDbHelper;

    /**
     * Constructor
     *
     * @param context Information about an application environment.
     */
    public ExternalPackageQueries(Context context) {
        this.context = context;
        this.dataPaths = new DataPaths(context);
        this.repositoryDbHelper = new RepositoryDatabaseHelper(context);
    }


    /**
     * @param packageName Name from the package
     * @return Information available about the package only make local queries
     */
    public PackageMetadata getPackageInfo(String packageName) {
        PackageMetadata pkg;
        ArrayList<PackageMetadata> dependencies;
        pkg = getPackageMetadataByName(packageName);
        loadPackageMetadataDependencies(pkg);
        return pkg;
    }


    /**
     * @param name
     * @return
     */
    private PackageMetadata getPackageMetadataByName(String name) {
        try {
            RuntimeExceptionDao<PackageMetadata, Integer> dao = repositoryDbHelper.getPackageMetadataDao();
            QueryBuilder<PackageMetadata, Integer> builder = dao.queryBuilder();
            Where<PackageMetadata, Integer> where = builder.where();
            where.eq(PackageMetadata.COLUMN_NAME_NAME, name);
            builder.limit(1l);

            return dao.queryForFirst(builder.prepare());
        } catch (java.sql.SQLException e) {
            Log.w(TAG, e);
            return null;
        }
    }

    /**
     * @param packageMetadata
     */
    private void loadPackageMetadataDependencies(PackageMetadata packageMetadata) {
        if (packageMetadata == null) {
            return;
        }
        try {
            RuntimeExceptionDao<Dependency, Integer> dao = repositoryDbHelper.getDependencyDao();
            QueryBuilder<Dependency, Integer> builder = dao.queryBuilder();
            Where<Dependency, Integer> where = builder.where();
            where.eq(Dependency.COLUMN_NAME_FOR_PACKAGE, packageMetadata);
            List<Dependency> dependencies = dao.query(builder.prepare());
            if (dependencies.size() > 0) {
                ArrayList<PackageMetadata> pkgDependencies = new ArrayList<PackageMetadata>(dependencies.size());
                for (Dependency dependency : dependencies) {
                    pkgDependencies.add(dependency.getFromPackage());
                }
                packageMetadata.setDependencies(pkgDependencies);
            } else {
                packageMetadata.setDependencies(null);
            }
        } catch (java.sql.SQLException e) {
            Log.w(TAG, e);
        }
    }
}
